﻿using Rabbit.Domain;
using Rabbit.Domain.Cultivation;
using Rabbit.Domain.Slaughter;
using Rabbit.DTO.Output;
using Rabbit.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rabbit.Service
{
    /// <summary>
    /// 屠宰信息
    /// </summary>
    public class SlaughterService
    {
        /// <summary>
        /// 省
        /// </summary>
        RabbitRepository<Province> _pres;
        /// <summary>
        /// 市
        /// </summary>
        RabbitRepository<City> _cres;
        /// <summary>
        /// 县
        /// </summary>
        RabbitRepository<County> _cxres;
        /// <summary>
        /// 屠宰相关图片
        /// </summary>
        RabbitRepository<SlaughterImg> _simgres;
        /// <summary>
        /// 屠宰信息
        /// </summary>
        RabbitRepository<SlaughterInfo> _sires;
        /// <summary>
        /// 屠宰企业信息
        /// </summary>
        RabbitRepository<SlaughterQY> _sqyres;
        /// <summary>
        /// 溯源管理
        /// </summary>
        RabbitRepository<TraceManage> _tres;

        public SlaughterService(RabbitRepository<Province> pres, RabbitRepository<City> cres,
            RabbitRepository<County> cxres, RabbitRepository<SlaughterImg> simgres,
            RabbitRepository<SlaughterQY> sqyres, RabbitRepository<SlaughterInfo> sires,
            RabbitRepository<TraceManage> tres)
        {
            _cres = cres;
            _cxres = cxres;
            _simgres = simgres;
            _sires = sires;
            _sqyres = sqyres;
            _pres = pres;
            _tres = tres;
        }


        #region 屠宰信息

        /// <summary>
        /// 屠宰信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<SlaughterInfo> GetSlaughter(int tmid)
        {
            return _sires.GetAll().Where(x => x.TMid == tmid).ToList();
        }
        /// <summary>
        /// 添加或修改屠宰信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddSlaughter(SlaughterInfo input)
        {
            var list = _sires.GetAll().FirstOrDefault(x => x.TMid == input.TMid);
            if (list != null)
            {
                //修改
                list.TMBatch = input.TMBatch;
                list.SIWeigth = input.SIWeigth;
                list.SIStaDate = input.SIStaDate;
                list.SIDate = input.SIDate;
                list.TMid = input.TMid;
                _sires.Updates(list);
                var lis=_tres.GetAll().FirstOrDefault(x=>x.TMid==input.TMid);
                if (lis != null)
                {
                    if (input.TMBatch != lis.TMBatch)
                    {
                        lis.TMBatch=input.TMBatch;
                        _tres.Updates(lis);
                    }
                }
                return 1;//修改成功
            }
            _sires.Add(input);
            return 2;//添加成功
        }





        #endregion


        #region 相关资质

        /// <summary>
        /// 添加相关资质
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddSlaughterImg(SlaughterImg input)
        {
            var list = new SlaughterImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.SImgurl = input.SImgurl;
            list.TMid = input.TMid;
            list.SImgState = 1;
            return _simgres.Add(list);
        }
        /// <summary>
        /// 图片相关资质
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<SlaughterImg> GetSlaughterImg(int tmid)
        {
            var data = _simgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1&&x.SImgState==1).ToList();
            return data;
        }
        /// <summary>
        /// 删除相关资质图片
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelSlaughterImg(int ids)
        {
            var list = _simgres.GetAll().Where(x => x.SImgid == ids).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelDate = DateTime.Now;
                item.DelName = "老八";
            }
            return _simgres.Update(list);
        }

        #endregion


        #region 企业信息

        /// <summary>
        /// 企业信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<SlaughterQYOutput> GetSlaughterQY(int tmid)
        {
            var list = (from a in  _sqyres.GetAll().Where(x => x.TMid == tmid)
                        join b in _pres.GetAll()
                        on a.Pid equals b.Pid
                        join c in _cres.GetAll()
                        on a.Cid equals c.Cid
                        join d in _cxres.GetAll()
                        on a.CXid equals d.CXid select
                        new SlaughterQYOutput
                        {
                            SQYid=a.SQYid,
                            SQYBrief=a.SQYBrief,
                            SQYDate=a.SQYDate,
                            SQYName=a.SQYName,
                            TMid=a.TMid,
                            Pid=a.Pid,
                            pname=b.PName,
                            Cid=a.Cid,
                            cname=c.CName,
                            CXid=a.CXid,
                            cxname=d.CXName
                        }).ToList();
            return list;
    }
        /// <summary>
        /// 添加或修改企业信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddSlaughterQY(SlaughterQY input)
        {
            var list = _sqyres.GetAll().FirstOrDefault(x => x.TMid == input.TMid);
            if (list != null)
            {
                //修改
                list.SQYName = input.SQYName;
                list.SQYDate = input.SQYDate;
                list.Pid = input.Pid;
                list.Cid = input.Cid;
                list.CXid = input.CXid;
                list.SQYBrief = input.SQYBrief;
                list.TMid = input.TMid;
                _sqyres.Updates(list);
                return 1;//修改成功
            }
            _sqyres.Add(input);
            return 2;//添加成功
        }

        /// <summary>
        /// 省
        /// </summary>
        /// <returns></returns>
        public List<Province> GetProvince()
        {
            return _pres.GetAll();
        }
        /// <summary>
        /// 市
        /// </summary>
        /// <returns></returns>
        public List<City> GetCity(int pid)
        {
            return _cres.GetAll().Where(x=>x.Pid==pid).ToList();
        }
        /// <summary>
        /// 县
        /// </summary>
        /// <returns></returns>
        public List<County> GetCounty(int cid)
        {
            return _cxres.GetAll().Where(x=>x.Cid==cid).ToList();
        }





        #endregion


        #region 屠宰企业图片

        /// <summary>
        /// 添加企业图片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddSlaughterImg2(SlaughterImg input)
        {
            var list = new SlaughterImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.SImgurl = input.SImgurl;
            list.TMid = input.TMid;
            list.SImgState = 2;
            return _simgres.Add(list);
        }
        /// <summary>
        /// 企业图片
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<SlaughterImg> GetSlaughterImg2(int tmid)
        {
            var data = _simgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.SImgState == 2).ToList();
            return data;
        }
        /// <summary>
        /// 删除企业图片
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelSlaughterImg2(int ids)
        {
            var list = _simgres.GetAll().Where(x => x.SImgid == ids).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelDate = DateTime.Now;
                item.DelName = "老八";
            }
            return _simgres.Update(list);
        }

        #endregion

    }
}
